package com.gxc.authcenter.system.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.domain.Role;
import com.gxc.authcore.system.service.RoleService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;
import com.gxc.webcore.util.JsonUtil;
import com.gxc.webcore.util.RequestUtil;
import com.gxc.webcore.util.StringUtil;

@Controller("system/roleController")
public class RoleController {

	@Autowired
	private RoleService roleService;

	/**
	 * 角色列表页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("system/role/role_list.vm");
	}

	/**
	 * 列出一页角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView grid(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = roleService.listPage(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 新建角色页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ModelAndView create(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("system/role/role_create.vm");
	}

	/**
	 * 新建角色保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Role role = (Role) RequestUtil.getInObject(request, Role.class);
		Role temp = roleService.retrieveByName(role.getName());
		if (temp != null) {
			result.put("status", "failed");
			result.put("data", "该角色已存在，请使用其他名称。");
			String resultString = JsonUtil.object2json(result);
			response.getWriter().write(resultString);
			return null;
		}
		role.setId("role_" + StringUtil.getUUID());
		roleService.create(role);
		result.put("status", "success");
		result.put("data", role);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView edit(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Role role = roleService.retrieve(id);
		Map model = new HashMap();
		model.put("role", role);
		return new ModelAndView("system/role/role_edit.vm", model);
	}

	/**
	 * 新建角色保存
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView editSave(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		Role role = (Role) RequestUtil.getInObject(request, Role.class);
		int i = roleService.update(role);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除一个角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		int i = roleService.delete(id);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除多个角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map result = new HashMap();
		String id = request.getParameter("id");
		String[] id_array = id.split(",");
		int i = roleService.delete(id_array);
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 查看角色
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView view(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		Role role = roleService.retrieve(id);
		Map model = new HashMap();
		model.put("role", role);
		return new ModelAndView("system/role/role_view.vm", model);
	}

	/**
	 * 检测账号是否已存在
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView checkExist(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = new HashMap();
		String name = request.getParameter("name");
		Role role = roleService.retrieveByName(name);
		if (role == null) {
			map.put("status", "success");
			map.put("data", "该角色名称有效。");
		} else {
			map.put("status", "failed");
			map.put("data", "该角色名称已存在。");
		}
		String jsonResult = JsonUtil.object2json(map);
		response.getWriter().write(jsonResult);
		return null;
	}

	/**
	 * 角色权限管理页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView listAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String role_id = request.getParameter("role_id");
		Role role = roleService.retrieve(role_id);
		Map model = new HashMap();
		model.put("role", role);
		return new ModelAndView("system/role/role_action_relate.vm", model);
	}

	/**
	 * 获取角色关联的action
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView gridAction(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map map = RequestUtil.getInMap(request);
		PageInfo pageInfo = PageInfo.getFromRequest(request);
		PageList pageList = roleService.listPageOfAction(map, pageInfo);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", pageList);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 添加角色与action的关联
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createActionRelate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String role_id = request.getParameter("role_id");
		String action_id = request.getParameter("action_id");
		int i = roleService.createActionRelate(role_id, action_id);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 添加角色与action的关联
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView createActionRelateBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String role_id = request.getParameter("role_id");
		String[] action_id = request.getParameter("action_id").split(",");
		int i = roleService.createActionRelateBatch(role_id, action_id);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除角色与action的关联
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteActionRelate(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String role_id = request.getParameter("role_id");
		String action_id = request.getParameter("action_id");
		int i = roleService.deleteActionRelate(role_id, action_id);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

	/**
	 * 删除角色与action的关联
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public ModelAndView deleteActionRelateBatch(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String role_id = request.getParameter("role_id");
		String action_id[] = request.getParameter("action_id").split(",");
		int i = roleService.deleteActionRelateBatch(role_id, action_id);
		Map result = new HashMap();
		result.put("status", "success");
		result.put("data", i);
		String resultString = JsonUtil.object2json(result);
		response.getWriter().write(resultString);
		return null;
	}

}
